from django.shortcuts import render
import requests
from bs4 import BeautifulSoup
import time
import socket
import shodan

def index(request):
    return render(request,'index.html')

def ipandport_search(request):
    return render(request,'ipandport-search.html')

def subdomain_search(request):
    return render(request, 'subdomain-search.html')

def whois_search(request):

    return render(request, 'whois-search.html')

def whois(request):
    request.encoding = 'utf-8'

    message = request.POST['url']
    whoisheaders = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
    }
    whoisr = requests.get('http://whois.chinaz.com/' + message, headers=whoisheaders)
    whoisres = whoisr.text
    whoissoup = BeautifulSoup(whoisres, 'html.parser')
    whoisa = whoissoup.find_all('p', class_="MoreInfo")
    if whoisa==[]:
        whoispresa = "没有查到内容"
    else:
        print(whoisa)
        whoisresa = str(whoisa[0])
        whoispresa = whoisresa.replace('<br/>', "\r\n")

        #whoisf = open('WhoisInfo.txt', "w+")
        #whoisf.write(whoispresa)
        #whoisf.close()
        print(whoispresa)

    return render(request,'whois.html',{'url':message,'result':whoispresa})

def subdomain(request):
    page=2
    domain = request.POST['url']
    res_list=[]
    url = 'http://global.bing.com/search?q=site%3A' + domain + '&qs=n&form=QBRE&sp=-1&pq=site%3A' + domain + '&sc=2-11&sk=&cvid=C1A7FC61462345B1A71F431E60467C43'

    headers = {
        'User-Agent': 'ozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4295.400 QQBrowser/9.7.12661.400',
        'Host': 'global.bing.com',
        'Referer': 'http://global.bing.com/?FORM=HPCNEN&setmkt=en-us&setlang=en-us',
        'Upgrade-Insecure-Requests': '1',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': 'DUP=Q=xa-EfMBM4gI7W690UHSTmQ2&T=312971513&A=2&IG=5161BB1CD80C4B8F8E04213C9A6BB2F1; MUID=35B121D519D96CA802AF2AE41DD96FD2; SRCHD=AF=HPCNEN; SRCHUID=V=2&GUID=59BB79AC0CDE4D8F853C004286CA05C4&dmnchg=1; SRCHUSR=DOB=20171201; MUIDB=35B121D519D96CA802AF2AE41DD96FD2; ULC=H=1D535|1:1&T=1D535|1:1; _RwBf=s=70&o=16; ipv6=hit=1512120707130&t=4; _EDGE_S=mkt=en-us&ui=en-us&SID=0AC562F3333D612722A469B832E160FE; SNRHOP=I=&TS=; _SS=SID=0AC562F3333D612722A469B832E160FE&HV=1512117115&R=0&bIm=473689; SRCHHPGUSR=CW=654&CH=997&DPR=1&UTC=480&WTS=63647713897'
    }
    for i in range(1, page):
        data = {
            'q': 'site:' + domain,
            'qs': 'n',
            'sp': '3',
            'sc': '0-12',
            'sk': '',
            'cvid': '710C7FF1A9B741C29D93EA4CCC435B27',
            'first': i * 12,
            'FORM': 'PERE'
        }
        sessions = requests.Session()
        results = sessions.get(url, headers=headers, params=data)
        soup = BeautifulSoup(results.content, 'html.parser')
        job_bt = soup.findAll('h2')
        for i in job_bt:
            res_list.append(i.a.get('href'))
        time.sleep(1)
    return render(request,'subdomain.html',{'url':request.POST['url'],'res_list':res_list})

def ipandport(request):
    domain = request.POST['url']
    api = shodan.Shodan('4lqyZddnTpSdLGoQxnNNsW0ny0a9tALc')
    myaddr = socket.getaddrinfo(domain, 'http')
    dst_ip = myaddr[0][4][0]
    #print(dst_ip)
    res = api.host(dst_ip, history=False)
    a = str(res['ports'])
    #print(a)
    return render(request,'ipandport.html',{'url':domain,'ip':dst_ip,'port':a})